package sendGoodsRecord

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetSendGoodsRecordPageApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetSendGoodsRecordPageApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetSendGoodsRecordPageApiLogic {
	hostname, _ := os.Hostname()
	return &GetSendGoodsRecordPageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetSendGoodsRecordPageApiLogic) GetSendGoodsRecordPageApi(req *types.GetSendGoodsRecordPageReq) (resp *types.GetSendGoodsRecordPageResp, err error) {
	// todo: add your logic here and delete this line
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return nil, err
	}
	result, count, err := service.SendGoodsRecordService.GetStoreSortingPageApi(l.ctx, bo.StoreSortingPageBoReq{
		StartTime:    req.StartTime,            // 开始时间 时间格式为:2024-12-12 12:12:12格式
		EndTime:      req.EndTime,              // 结束时间 时间格式为:2024-12-12 12:12:12格式
		OrderNo:      req.OrderNo,              // 出库流水号
		PageSize:     req.PageSize,             // 当前条数
		PageNumber:   req.PageNumber,           // 当前页数
		EnterpriseID: accountInfo.EnterpriseId, // 关联到enterprise表主键id
		CustomerID:   req.CustomerID,           // 客户id
	})
	if err != nil {
		return &types.GetSendGoodsRecordPageResp{
			Data: make([]types.SendGoodsRecordModel, 0),
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
		}, nil
	}
	var result1 = make([]types.SendGoodsRecordModel, 0)
	for _, item := range result {
		result1 = append(result1, types.SendGoodsRecordModel{
			ID:            item.ID,                                 // 主键id
			OrderNo:       item.OrderNo,                            // 订单流水号
			CustomerID:    item.CustomerID,                         // 客户id,关联到enterprise_customer表主键id
			CustomerName:  item.CustomerName,                       // 客户id,关联到enterprise_customer表主键id
			TotalAmount:   item.TotalAmount.StringFixed(2),         // 订单金额
			TotalCount:    item.TotalCount.StringFixed(2),          // 订单商品数量
			WarehouseID:   item.WarehouseID,                        // 出库仓库,关联到warehouse表主键id
			WarehouseName: item.WarehouseName,                      // 出库仓库,关联到warehouse表主键id
			OrdersID:      item.OrdersID,                           // 关联到订单表id,orders表主键id
			OrdersOrderNo: item.OrdersOrderNo,                      // 关联到订单表id,orders表主键id
			OperateName:   item.OperateName,                        // 制单员
			OperateID:     item.OperateID,                          // 制单员id,关联到员工表
			Remark:        item.Remark,                             // 备注
			CreatedAt:     utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:     utils.TimestampToString(item.UpdatedAt), // 更新时间
		})
	}
	return &types.GetSendGoodsRecordPageResp{
		Data: result1,
		PageBaseResp: types.PageBaseResp{
			Total:      count,
			PageSize:   req.PageSize,
			PageNumber: req.PageNumber,
		},
	}, nil
}
